package com.yxhl.zoume.core.tripsmgmt.presenter.home;

import android.content.Context;

import com.yxhl.protobuf.BizOrder;
import com.yxhl.protobuf.BizOrderRequest;
import com.yxhl.protobuf.BizOrderResponse;
import com.yxhl.protobuf.ClientType;
import com.yxhl.protobuf.CommonReqParams;
import com.yxhl.protobuf.CommonRespParams;
import com.yxhl.protobuf.PageOption;
import com.yxhl.protobuf.TravelType;
import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.common.ui.view.UpPullRefreshView;
import com.yxhl.zoume.core.tripsmgmt.presenter.ipresenter.ICurrentTripsPresenter;
import com.yxhl.zoume.core.tripsmgmt.view.home.CurrentTripsView;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.tripsmgmt.CurrentTripOrdersUseCase;

import java.util.List;

import javax.inject.Inject;

import rx.Subscription;

/**
 * 项目名称：Ferrari
 * Created by menggod on 16/11/29 .
 */
public class CurrentTripsPresenter extends BasePresenter implements ICurrentTripsPresenter {

    /**
     * 一次上拉加载的item条目数
     */
    public static final int PAGE_SIZE = 15;
    private int currentPage = 1;

    private static final String TAG = "CurrentTripsPresenter";
    @Inject
    CurrentTripOrdersUseCase mCurrentTripOrdersUseCase;

    private CurrentTripsView mCurrentTripsView;
    private UpPullRefreshView<BizOrder> mUpPullRefreshView;
    /**
     * 一次上拉加载的item条目数
     */
    private Subscription mSubscription;

    @Inject
    public CurrentTripsPresenter(Context context) {
        super(context);
    }

    private BizOrderRequest getBizOrderRequest(TravelType travelType, boolean isRefresh) {
        if (isRefresh) {
            currentPage = 1;
        }
        CommonReqParams.Builder commonReqParamsBuilder = CommonReqParams.newBuilder();
        commonReqParamsBuilder.setClientType(ClientType.USER).setToken(getToken());

        PageOption.Builder pageOptionBuilder = PageOption.newBuilder();
        pageOptionBuilder.setCurrentPage(currentPage++).setPageSize(PAGE_SIZE);

        BizOrderRequest.Builder bizOrderRequestBuilder = BizOrderRequest.newBuilder();
        bizOrderRequestBuilder.setCommonReqParams(commonReqParamsBuilder)
                .setPageOption(pageOptionBuilder)
                .setType(travelType)
                .setMobile(getMobile());

        return bizOrderRequestBuilder.build();
    }

    public void queryCurrentTripsData(boolean isRefresh) {
        //订单状态为已下单,已支付,部分退款中,部分已完成落入当前行程列表
        final BizOrderRequest bizOrderRequest = getBizOrderRequest(TravelType.TT2_NOW, isRefresh);

        mCurrentTripOrdersUseCase.execute(bizOrderRequest, new ZMSubscriber<BizOrderResponse>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
                mCurrentTripsView.closeDownRefresh();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mCurrentTripsView.closeDownRefresh();
                mCurrentTripsView.renderDataOnError();
            }

            @Override
            public void onNext(BizOrderResponse bizOrderResponse) {
                super.onNext(bizOrderResponse);
                if (bizOrderResponse != null) {
                    CommonRespParams commonRespParams = bizOrderResponse.getCommonRespParams();
                    String isSucc = commonRespParams.getIsSucc();
                    if ("true".equalsIgnoreCase(isSucc)) {
                        List<BizOrder> bizOrderList = bizOrderResponse.getOrderFieldsesList();
                        mCurrentTripsView.renderDataOnSuccess(bizOrderList);
                    } else {
                        mCurrentTripsView.renderDataOnFailure(commonRespParams.getResultMsg());
                    }
                }
            }
        });
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
    }

    @Override
    public void onDestroy() {
        mCurrentTripOrdersUseCase.unsubscribe();
    }

    @Override
    public void attachView(BaseView view) {
        if (view instanceof CurrentTripsView) {
            mCurrentTripsView = (CurrentTripsView) view;
        }
        if (view instanceof UpPullRefreshView) {
            mUpPullRefreshView = (UpPullRefreshView<BizOrder>) view;
        }
    }
}
